<template>
  <div class="profile">
    <div class="header">
      <div class="container">
        <div class="header-content">
          <h1 class="logo">Vue2 Auth System</h1>
          <div class="nav">
            <el-button type="text" @click="$router.push('/dashboard')">首页</el-button>
            <el-button type="text" @click="$router.push('/profile')">个人资料</el-button>
            <el-button type="text" @click="handleLogout" class="logout-btn">退出登录</el-button>
          </div>
        </div>
      </div>
    </div>
    
    <div class="main-content">
      <div class="container">
        <h2 class="page-title">个人资料</h2>
        
        <div class="card profile-card">
          <el-row :gutter="30">
            <el-col :span="8">
              <div class="avatar-section">
                <el-avatar :size="100" :src="avatarUrl" class="avatar"></el-avatar>
                <h3>{{ userInfo.name }}</h3>
                <p>{{ userInfo.role === 'admin' ? '管理员' : '普通用户' }}</p>
              </div>
            </el-col>
            
            <el-col :span="16">
              <div class="info-section">
                <h3>基本信息</h3>
                <el-form label-width="80px">
                  <el-form-item label="用户名">
                    <el-input v-model="userInfo.username" readonly></el-input>
                  </el-form-item>
                  <el-form-item label="姓名">
                    <el-input v-model="userInfo.name" readonly></el-input>
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="userInfo.email" readonly></el-input>
                  </el-form-item>
                  <el-form-item label="角色">
                    <el-input v-model="roleText" readonly></el-input>
                  </el-form-item>
                  <el-form-item label="用户ID">
                    <el-input v-model="userInfo.id" readonly></el-input>
                  </el-form-item>
                </el-form>
              </div>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserInfo, clearAuth } from '@/utils/token'

export default {
  name: 'Profile',
  data() {
    return {
      userInfo: getUserInfo() || {},
      avatarUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
    }
  },
  computed: {
    roleText() {
      return this.userInfo.role === 'admin' ? '管理员' : '普通用户'
    }
  },
  methods: {
    handleLogout() {
      this.$confirm('确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        clearAuth()
        this.$message.success('已退出登录')
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style scoped>
.profile {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.header {
  background: white;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
}

.logo {
  color: #409EFF;
  font-size: 24px;
  font-weight: 700;
}

.nav .el-button {
  margin-left: 10px;
}

.logout-btn {
  color: #F56C6C;
}

.logout-btn:hover {
  color: #f78989;
}

.main-content {
  padding: 20px 0;
}

.page-title {
  color: #333;
  margin-bottom: 30px;
  font-size: 28px;
}

.profile-card {
  padding: 30px;
}

.avatar-section {
  text-align: center;
  padding: 20px 0;
}

.avatar {
  margin-bottom: 15px;
}

.avatar-section h3 {
  margin: 10px 0 5px;
  color: #333;
}

.avatar-section p {
  color: #666;
}

.info-section h3 {
  color: #333;
  margin-bottom: 20px;
  font-size: 18px;
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    text-align: center;
  }
  
  .nav {
    margin-top: 15px;
  }
  
  .profile-card .el-col {
    margin-bottom: 20px;
  }
}
</style>